package cmd

import (
	"github.com/spf13/cobra"
	"os"
)

// 自动补全命令
// TODO 在MAC上未调通
func completionCmd() *cobra.Command {
	var tmp = &cobra.Command{
		Use:   "completion",
		Short: "命令自动补全",
		Long: `Output shell completion code for the specified shell (bash or zsh).
		The shell code must be evaluated to provide interactive
		completion of opsctl commands.  This can be done by sourcing it from
		the .bash_profile.
		Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2`,
		Example: `
		# Installing bash completion on macOS using homebrew
		## If running Bash 3.2 included with macOS
		    brew install bash-completion
		## or, if running Bash 4.1+
		    brew install bash-completion@2
		## If opsctl is installed via homebrew, this should start working immediately.
		## If you've installed via other means, you may need add the completion to your completion directory
		    opsctl completion bash > $(brew --prefix)/etc/bash_completion.d/opsctl


		# Installing bash completion on Linux
		## If bash-completion is not installed on Linux, please install the 'bash-completion' package
		## via your distribution's package manager.
		## Load the opsctl completion code for bash into the current shell
		    source <(opsctl completion bash)
		## Write bash completion code to a file and source if from .bash_profile
		    opsctl completion bash > ~/.kube/completion.bash.inc
		    printf "
		      # opsctl shell completion
		      source '$HOME/.kube/completion.bash.inc'
		      " >> $HOME/.bash_profile
		    source $HOME/.bash_profile

		# Load the opsctl completion code for zsh[1] into the current shell
		    source <(opsctl completion zsh)
		# Set the opsctl completion code for zsh[1] to autoload on startup
		    opsctl completion zsh > "${fpath[1]}/_opsctl"`,
		ValidArgs:             []string{"bash", "zsh"},
		DisableFlagsInUseLine: true,
		Args:                  cobra.ExactValidArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
			switch args[0] {
			case "bash":
				_ = cmd.Root().GenBashCompletion(os.Stdout)
			case "zsh":
				_ = cmd.Root().GenZshCompletion(os.Stdout)
			}
		},
	}
	return tmp
}
